#region Usings
using System;
using System.Globalization;
using System.Text;
using System.Xml.Serialization;
#endregion

namespace Rolodex.Model
{
    public class Card
    {
        #region Members
        private string _Title = String.Empty;
        private Char _Letter;
        private string _AddressLine1 = String.Empty;
        private string _AddressLine2 = String.Empty;
        private string _AddressLine3 = String.Empty;
        private string _AddressLine4 = String.Empty;
        private string _PostCode = String.Empty;
        private string _TelephoneNumber = String.Empty;
        private string _TelephoneNumberSpeedDial = String.Empty;
        private string _Extension = String.Empty;
        private string _MobileNumber = String.Empty;
        private string _MobileNumberSpeedDial = String.Empty;
        private string _FaxNumber = String.Empty;
        private string _EmailAddress = String.Empty;
        private string _ContactName = String.Empty;
        private string _Notes = String.Empty;
        #endregion

        #region Properties
        [XmlElement("title")]
        public string Title
        {
            get { return this._Title.Trim(); }
            set { this._Title = value; }
        }

        [XmlElement("letter")]
        public char Letter
        {
            get { return this._Letter; }
            set { this._Letter = value; }
        }

        [XmlElement("addressline1")]
        public string AddressLine1
        {
            get { return this._AddressLine1.Trim(); }
            set { this._AddressLine1 = value; }
        }

        [XmlElement("addressline2")]
        public string AddressLine2
        {
            get { return this._AddressLine2.Trim(); }
            set { this._AddressLine2 = value; }
        }

        [XmlElement("addressline3")]
        public string AddressLine3
        {
            get { return this._AddressLine3.Trim(); }
            set { this._AddressLine3 = value; }
        }

        [XmlElement("addressline4")]
        public string AddressLine4
        {
            get { return this._AddressLine4.Trim(); }
            set { this._AddressLine4 = value; }
        }

        [XmlElement("postcode")]
        public string PostCode
        {
            get { return this._PostCode.Trim(); }
            set { this._PostCode = value; }
        }

        [XmlElement("telephone")]
        public string TelephoneNumber
        {
            get { return this._TelephoneNumber.Trim(); }
            set { this._TelephoneNumber = value; }
        }

        [XmlElement("telephonespeeddial")]
        public string TelephoneNumberSpeedDial
        {
            get { return this._TelephoneNumberSpeedDial.Trim(); }
            set { this._TelephoneNumberSpeedDial = value; }
        }

        [XmlElement("extension")]
        public string Extension
        {
            get { return this._Extension.Trim(); }
            set { this._Extension = value; }
        }

        [XmlIgnore]
        public string TelephoneNumberLabel
        {
            get
            {
                StringBuilder theTelephoneNumber = new StringBuilder();

                if (!String.IsNullOrEmpty(this._TelephoneNumber))
                {
                    theTelephoneNumber.AppendFormat(
                        CultureInfo.CurrentCulture,
                        "Tel: {0}",
                        this._TelephoneNumber.Trim());
                }
                if (!String.IsNullOrEmpty(this._Extension))
                {
                    theTelephoneNumber.AppendFormat(
                        CultureInfo.CurrentCulture,
                        " (Ext: {0})",
                        this._Extension.Trim());
                }
                if (!String.IsNullOrEmpty(this._TelephoneNumberSpeedDial))
                {
                    theTelephoneNumber.AppendFormat(
                        CultureInfo.CurrentCulture,
                        " (SD: {0})",
                        this._TelephoneNumberSpeedDial.Trim());
                }

                return theTelephoneNumber.ToString();
            }
        }

        [XmlElement("mobile")]
        public string MobileNumber
        {
            get { return this._MobileNumber.Trim(); }
            set { this._MobileNumber = value; }
        }

        [XmlElement("mobilespeeddial")]
        public string MobileNumberSpeedDial
        {
            get { return this._MobileNumberSpeedDial.Trim(); }
            set { this._MobileNumberSpeedDial = value; }
        }

        [XmlIgnore]
        public string MobileNumberLabel
        {
            get
            {
                StringBuilder theMobileNumber = new StringBuilder();

                if (!String.IsNullOrEmpty(this._MobileNumber))
                {
                    theMobileNumber.AppendFormat(
                        CultureInfo.CurrentCulture,
                        "Mob: {0}",
                        this._MobileNumber.Trim());
                }
                if (!String.IsNullOrEmpty(this._MobileNumberSpeedDial))
                {
                    theMobileNumber.AppendFormat(
                        CultureInfo.CurrentCulture,
                        " (SD: {0})",
                        this._MobileNumberSpeedDial.Trim());
                }

                return theMobileNumber.ToString();
            }
        }

        [XmlElement("fax")]
        public string FaxNumber
        {
            get { return this._FaxNumber.Trim(); }
            set { this._FaxNumber = value; }
        }

        [XmlIgnore]
        public string FaxNumberLabel
        {
            get
            {
                if (!String.IsNullOrEmpty(this._FaxNumber))
                {
                    return String.Format(
                        CultureInfo.CurrentCulture,
                        "Fax: {0}",
                        this._FaxNumber.Trim());
                }
                return String.Empty;
            }
        }

        [XmlElement("email")]
        public string EmailAddress
        {
            get { return this._EmailAddress.Trim(); }
            set { this._EmailAddress = value; }
        }

        [XmlElement("contact")]
        public string ContactName
        {
            get { return this._ContactName.Trim(); }
            set { this._ContactName = value; }
        }

        [XmlIgnore]
        public string ContactNameLabel
        {
            get
            {
                if (!String.IsNullOrEmpty(this._ContactName))
                {
                    return String.Format(
                        CultureInfo.CurrentCulture,
                        "Contact: {0}",
                        this._ContactName.Trim());
                }
                return String.Empty;
            }
        }

        [XmlElement("notes")]
        public string Notes
        {
            get { return this._Notes.Trim(); }
            set { this._Notes = value; }
        }
        #endregion

        #region Methods
        #region Constructors
        public Card() { }
        #endregion

        #region Public Helpers
        public bool IsMatch(string theCriteria)
        {
            if (!String.IsNullOrEmpty(theCriteria))
            {
                theCriteria = theCriteria.ToLower(CultureInfo.CurrentCulture);

                return this._AddressLine1.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._AddressLine2.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._AddressLine3.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._AddressLine4.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._ContactName.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._EmailAddress.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._FaxNumber.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._MobileNumber.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._Notes.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._PostCode.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._TelephoneNumber.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._Extension.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria) ||
                    this._Title.ToLower(CultureInfo.CurrentCulture).Contains(theCriteria);
            }
            return false;
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
        #endregion
    }
}
